package com.loongtech.bi.action.flow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

@Controller
@Scope("view")
public class AuanyCreateorderFlowAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
    protected DecimalFormat df = new DecimalFormat("######0.00");
    protected Date dateStart = new Date(Time.getBeginningOfDay(Time
            .currentTime()));
    protected Date dateEnd = new Date(
            Time.getBeginningOfDay(Time.currentTime()));

    private AuanyCreateorderFlow queryBean; // 查询bean

    private List<Object[]> result; // 结果List

    @Autowired
    HadoopQueryManager hadoopQueryManager;

    @Autowired
    private SysLogManager sysLogManager;
    @Autowired
    private Session session;

    @PostConstruct
    public void init() {
        queryBean = new AuanyCreateorderFlow();
        result = new ArrayList<>();
    }

    public void search() {
        sysLogManager.addLog(session.getUserName(), "流水查询—创建订单流水");

        result = new ArrayList<>();

        FacesContext context = FacesContext.getCurrentInstance();

        // 开始结束时间不能大于当前时间60天
        if (Time.getInterval(new Date(), dateStart) > 60
                || Time.getInterval(new Date(), dateEnd) > 60) {
            context.addMessage(null, new FacesMessage("提示",
                    "开始结束时间不能小于当前时间60天!"));
            return;
        }

        // 全用户名，角色ID必须至少有一个非空!
        if (StringUtils.isBlank(queryBean.getFulluseridentity())
                && StringUtils.isBlank(queryBean.getRoleid())) {
            context.addMessage(null, new FacesMessage("提示",
                    "全用户名，角色ID必须至少有一个非空!"));
            return;
        }

        if (HadoopQueryManager.VISIT_QUEUE.offer("AuanyCreateorderFlowAction")) {
            System.out.println("VISIT_QUEUE offer AuanyCreateorderFlowAction ok!");

            StringBuffer sql = new StringBuffer();
            sql.append("select logtime,hostname,modulename,gameid,auanyserverid,logid,logtag,ordertype,fulluseridentity,zoneid,roleid,gameproductid,gameproductnum,gamecurrencytype,gameproductamount,gameorderid,callbackurl,extra1,extra2 from auany_createorder where 1=1 ");
            if (!StringUtils.isBlank(queryBean.getRoleid())) {
                sql.append(" and roleid = '");
                sql.append(queryBean.getRoleid());
                sql.append("' ");
            }
            if (!"0".equals(queryServer)) {
                sql.append(" and zoneid = '");
                sql.append(queryServer);
                sql.append("' ");
            }
            if (queryBean.getAuanyserverid() != null
                    && !"".equals(queryBean.getAuanyserverid())) {
                sql.append(" and auanyserverid = '");
                sql.append(queryBean.getAuanyserverid().trim());
                sql.append("' ");
            }
            sql.append(" and ordertype = ");
            sql.append(queryBean.getOrdertype());
            if (queryBean.getFulluseridentity() != null
                    && !"".equals(queryBean.getFulluseridentity())) {
                sql.append(" and fulluseridentity = '");
                sql.append(queryBean.getFulluseridentity().trim());
                sql.append("' ");
            }
            if (queryBean.getGameproductid() != null
                    && !"".equals(queryBean.getGameproductid())) {
                sql.append(" and gameproductid = '");
                sql.append(queryBean.getGameproductid().trim());
                sql.append("' ");
            }
            if (queryBean.getGameorderid() != null
                    && !"".equals(queryBean.getGameorderid())) {
                sql.append(" and gameorderid = '");
                sql.append(queryBean.getGameorderid().trim());
                sql.append("' ");
            }
            sql.append(" and dt >= '");
            sql.append(Time.getDateStr(dateStart));
            sql.append("' and dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' ");
            sql.append(" order by logtime desc ");

            System.out.println(sql.toString());
            Entry<List<String>, List<Object[]>> ret = hadoopQueryManager
                    .executeSQL(sql.toString());
            if (null == ret || null == ret.getKey() || ret.getKey().isEmpty()
                    || null == ret.getValue()) {
                context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
                return;
            }

            result = ret.getValue();
        } else {
            System.out.println("VISIT_QUEUE offer AuanyCreateorderFlowAction failed!");
            context.addMessage(null, new FacesMessage("提示",
                    "当前系统正被访问，为保证服务器稳定，请稍后再试"));
            return;
        }
    }

    public class AuanyCreateorderFlow {
        private String dt; // 日期
        private String logtime; // 时间
        private String hostname; // 服务器主机名
        private String modulename; // 模块名
        private String gameid; // 游戏id
        private String auanyserverid; // au服务器编号
        private String logid; // 日志id
        private String logtag; // 日志类型
        private int ordertype = 1; // 订单类型
        private String fulluseridentity;// 全用户名
        private String zoneid; // 区服id
        private String roleid; // 角色id
        private String gameproductid; // 订单产品id
        private int gameproductnum; // 订单产品数量
        private String gamecurrencytype;// 订单货币类型
        private int gameproductamount; // 订单总金额
        private String gameorderid; // 订单id
        private String callbackurl; // 回调地址
        private String extra1; // 扩展字段1
        private String extra2; // 扩展字段2

        public String getDt() {
            return dt;
        }

        public void setDt(String dt) {
            this.dt = dt;
        }

        public String getLogtime() {
            return logtime;
        }

        public void setLogtime(String logtime) {
            this.logtime = logtime;
        }

        public String getHostname() {
            return hostname;
        }

        public void setHostname(String hostname) {
            this.hostname = hostname;
        }

        public String getModulename() {
            return modulename;
        }

        public void setModulename(String modulename) {
            this.modulename = modulename;
        }

        public String getGameid() {
            return gameid;
        }

        public void setGameid(String gameid) {
            this.gameid = gameid;
        }

        public String getAuanyserverid() {
            return auanyserverid;
        }

        public void setAuanyserverid(String auanyserverid) {
            this.auanyserverid = auanyserverid;
        }

        public String getLogid() {
            return logid;
        }

        public void setLogid(String logid) {
            this.logid = logid;
        }

        public String getLogtag() {
            return logtag;
        }

        public void setLogtag(String logtag) {
            this.logtag = logtag;
        }

        public int getOrdertype() {
            return ordertype;
        }

        public void setOrdertype(int ordertype) {
            this.ordertype = ordertype;
        }

        public String getFulluseridentity() {
            return fulluseridentity;
        }

        public void setFulluseridentity(String fulluseridentity) {
            this.fulluseridentity = fulluseridentity;
        }

        public String getZoneid() {
            return zoneid;
        }

        public void setZoneid(String zoneid) {
            this.zoneid = zoneid;
        }

        public String getRoleid() {
            return roleid;
        }

        public void setRoleid(String roleid) {
            this.roleid = roleid;
        }

        public String getGameproductid() {
            return gameproductid;
        }

        public void setGameproductid(String gameproductid) {
            this.gameproductid = gameproductid;
        }

        public int getGameproductnum() {
            return gameproductnum;
        }

        public void setGameproductnum(int gameproductnum) {
            this.gameproductnum = gameproductnum;
        }

        public String getGamecurrencytype() {
            return gamecurrencytype;
        }

        public void setGamecurrencytype(String gamecurrencytype) {
            this.gamecurrencytype = gamecurrencytype;
        }

        public int getGameproductamount() {
            return gameproductamount;
        }

        public void setGameproductamount(int gameproductamount) {
            this.gameproductamount = gameproductamount;
        }

        public String getGameorderid() {
            return gameorderid;
        }

        public void setGameorderid(String gameorderid) {
            this.gameorderid = gameorderid;
        }

        public String getCallbackurl() {
            return callbackurl;
        }

        public void setCallbackurl(String callbackurl) {
            this.callbackurl = callbackurl;
        }

        public String getExtra1() {
            return extra1;
        }

        public void setExtra1(String extra1) {
            this.extra1 = extra1;
        }

        public String getExtra2() {
            return extra2;
        }

        public void setExtra2(String extra2) {
            this.extra2 = extra2;
        }

    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    public AuanyCreateorderFlow getQueryBean() {
        return queryBean;
    }

    public void setQueryBean(AuanyCreateorderFlow queryBean) {
        this.queryBean = queryBean;
    }

    public List<Object[]> getResult() {
        return result;
    }

    public void setResult(List<Object[]> result) {
        this.result = result;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }
}
